<?php

namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class IndexController extends Controller
{
    public function index()
    {
        return view('home.index');
    }

    public function aboutUs()
    {
        return view('home.aboutUs');
    }

    public function fangchengmi()
    {
        return view('home.fangchengmi');
    }

    public function jiaoyijiufeng()
    {
        return view('home.jiaoyijiufeng');
    }

    public function jiazhangjianhu()
    {
        return view('home.jiazhangjianhu');
    }

    public function lianxi()
    {
        return view('home.lianxi');
    }

    public function yonghuxieyi()
    {
        return view('home.yonghuxieyi');
    }

    public function pay()
    {
        return view('home.pay');
    }

    public function reg()
    {
        return view('home.reg');
    }

    public function doReg(Request $request)
    {
        //获取用户信息
        $account = $request->get('account');
        $pswd = $request->get('pswd');
        $name = $request->get('name');
        $idcard = $request->get('idcard');
        //判断用户名是否存在
        $u = User::where('account',$account)->get()->toArray();
        if ($u){
            return 2;
        }
        $Users = new User();
            $Users->account = $account;
            $Users->password = Hash::make($pswd);
            $Users->name = $name;
            $Users->idcard = $idcard;
        $res = $Users->save();
        if ($res){
            return 1;
        } else {
            return 3;
        }
    }

    public function login()
    {
        return view('home.login');
    }

    public function doLogin(Request $request)
    {
        //判断是否为空
        $account = $request->get('account');
        $password = $request->get('pswd');
        //判断是否为空
        if (empty($account) || empty($password)){
            return 3;
        }
        //搜索数据库
        $res = User::where('account',$account)->first()->toArray();
        if (empty($res)){
            return 2;
        }
        //何时密码
        if (Hash::check($password, $res['password'])){
            session([
                'yj_home_id' => $res['id'],
                'yj_home_account' => $res['account']
            ]);
            return 1;
        } else {
            return 2;
        }
    }


    public function logout(Request $request)
    {
        $request->session()->forget('yj_home_id');
        $request->session()->forget('yj_home_account');
        return redirect('/');
    }
}
